﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web.UI.WebControls;

namespace HospitalManager.QuanLyTiepNhan.TiepNhanBenhNhan
{
    public partial class DanhSachMaBenhICD : System.Web.UI.Page
    {
        private const int PageSize = 20;
        private Module.QuanTriHeThong.DanhMuc.DMMaBenhICD _dmMaBenhIcd = new Module.QuanTriHeThong.DanhMuc.DMMaBenhICD();
        private int _totalrow;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            if (Session["username"] == null)
            {
                Response.RedirectToRoute("DangNhap");
            }
            LoadPage();
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            ViewState["CheckRefresh"] = Session["CheckRefresh"];
        }

        private void LoadPage()
        {
            _dmMaBenhIcd = new Module.QuanTriHeThong.DanhMuc.DMMaBenhICD
            {
                MaBenh = txtMaBenh.Text.Trim()
            };
            gvDanhMuc.DataSource = _dmMaBenhIcd.SelectAll(_dmMaBenhIcd, Convert.ToInt32(hfPageIndex.Value.Trim()), PageSize, out _totalrow);
            gvDanhMuc.DataBind();
            GeneratePager(_totalrow, PageSize, Convert.ToInt32(hfPageIndex.Value.Trim()));
            Session["CheckRefresh"] = Server.UrlEncode(DateTime.Now.ToString(CultureInfo.InvariantCulture));
        }

        protected void dlPager_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName != "PageNo") return;
            hfPageIndex.Value = e.CommandArgument.ToString();
            LoadPage();
            e.Item.Attributes.Add("disabled", "disabled");
        }

        public void GeneratePager(int totalRowCount, int pageSize, int currentPage)
        {
            const int totalLinkInPage = 4;
            var totalPageCount = (int)Math.Ceiling((decimal)totalRowCount / pageSize);

            var startPageLink = Math.Max(currentPage - (int)Math.Floor((decimal)totalLinkInPage / 2), 1);
            var lastPageLink = Math.Min(startPageLink + totalLinkInPage - 1, totalPageCount);

            if ((startPageLink + totalLinkInPage - 1) > totalPageCount)
            {
                lastPageLink = Math.Min(currentPage + (int)Math.Floor((decimal)totalLinkInPage / 2), totalPageCount);
                startPageLink = Math.Max(lastPageLink - totalLinkInPage + 1, 1);
            }

            var pageLinkContainer = new List<ListItem>();

            if (startPageLink != 1)
            {
                pageLinkContainer.Add(new ListItem("«", "1", currentPage != 1));
            }
            for (var i = startPageLink; i <= lastPageLink; i++)
            {
                pageLinkContainer.Add(new ListItem(i.ToString(CultureInfo.InvariantCulture),
                    i.ToString(CultureInfo.InvariantCulture), currentPage != i));
            }
            if (lastPageLink != totalPageCount)
                pageLinkContainer.Add(new ListItem("»", totalPageCount.ToString(CultureInfo.InvariantCulture),
                    currentPage != totalPageCount));

            dlPager.DataSource = pageLinkContainer;
            dlPager.DataBind();
        }

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            hfPageIndex.Value = "1";
            LoadPage();
        }
    }
}